using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using DMO = SQLDMO;

namespace SQLDIMOK
{
	/// <summary>
	/// Summary description for DBObjectsGrid.
	/// </summary>
	public class DBObjectsGrid : System.Windows.Forms.Form
	{
		object m = System.Reflection.Missing.Value;
		public System.Windows.Forms.DataGrid dgDBObjects;
		private System.Windows.Forms.ContextMenu cmnuDBObject;
		private System.Windows.Forms.MenuItem cmnuShow;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private string DBServer;
		private System.Windows.Forms.MenuItem menuItem1;
		private string DBName;
		private string Path;

		public DBObjectsGrid(string path,string dbServer,string dbName):this(){
			Path = path;
			DBServer = dbServer;
			DBName = dbName;
		}
		private DBObjectsGrid()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.dgDBObjects = new System.Windows.Forms.DataGrid();
			this.cmnuDBObject = new System.Windows.Forms.ContextMenu();
			this.cmnuShow = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			((System.ComponentModel.ISupportInitialize)(this.dgDBObjects)).BeginInit();
			this.SuspendLayout();
			// 
			// dgDBObjects
			// 
			this.dgDBObjects.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.dgDBObjects.CaptionVisible = false;
			this.dgDBObjects.ContextMenu = this.cmnuDBObject;
			this.dgDBObjects.DataMember = "";
			this.dgDBObjects.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dgDBObjects.Location = new System.Drawing.Point(0, 0);
			this.dgDBObjects.Name = "dgDBObjects";
			this.dgDBObjects.Size = new System.Drawing.Size(352, 288);
			this.dgDBObjects.TabIndex = 0;
			// 
			// cmnuDBObject
			// 
			this.cmnuDBObject.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																																								 this.cmnuShow,
																																								 this.menuItem1});
			// 
			// cmnuShow
			// 
			this.cmnuShow.Index = 0;
			this.cmnuShow.Text = "Show";
			this.cmnuShow.Click += new System.EventHandler(this.cmnuShow_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.Text = "Depends";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// DBObjectsGrid
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(348, 284);
			this.Controls.Add(this.dgDBObjects);
			this.Name = "DBObjectsGrid";
			this.Text = "DB Objects";
			((System.ComponentModel.ISupportInitialize)(this.dgDBObjects)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private string CurrentObjectName{
			get{return CurrentObjectsRow[0]+"";}
			set{CurrentObjectsRow[0] = value;}
		}
		private DataRow CurrentObjectsRow{
			get{
				return ((DataTable)dgDBObjects.DataSource).Rows[dgDBObjects.CurrentCell.RowNumber];
				//return ((DBObjectsDS.ObjectsDataTable)dgDBObjects.DataSource)[dgDBObjects.CurrentCell.RowNumber];
			}
		}
		private void cmnuShow_Click(object sender, System.EventArgs e) {
			using(SQLDMONET dmo = new SQLDMONET(DBServer)){
				MessageBox.Show(dmo.ObjectText(Path,DBName,CurrentObjectName,SQLDMONET.DB_SCRIPT_TYPE_ALL,m,false));
			}
		}

		private void menuItem1_Click(object sender, System.EventArgs e) {
			DBObjectsGrid form = new DBObjectsGrid(Path,DBServer,DBName);
			form.dgDBObjects.DataSource = SQLDMONET.Depends(DBServer,DBName,CurrentObjectName);
			form.Show();
		}
	}
}
